home *** CD-ROM | disk | FTP | other *** search
- unit ServAuto;
-
- interface
-
- uses
- ComObj, ActiveX, AxCtrls, Server_TLB;
-
- type
- TServerWithEvents = class(TAutoObject, IConnectionPointContainer, IServerWithEvents)
- private
- { Private declarations }
- FConnectionPoints: TConnectionPoints;
- FObjRegHandle: Integer;
- procedure MemoChange(Sender: TObject);
- protected
- { Protected declarations }
- procedure AddText(const NewText: WideString); safecall;
- procedure Clear; safecall;
- function GetConnectionEnumerator: IEnumConnections;
- property ConnectionPoints: TConnectionPoints read FConnectionPoints
- implements IConnectionPointContainer;
- public
- destructor Destroy; override;
- procedure Initialize; override;
- end;
-
- implementation
-
- uses Windows, ComServ, ServMain, SysUtils, StdCtrls;
-
- destructor TServerWithEvents.Destroy;
- begin
- inherited Destroy;
- RevokeActiveObject(FObjRegHandle, nil); // Make sure I'm removed from ROT
- end;
-
- procedure TServerWithEvents.Initialize;
- begin
- inherited Initialize;
- FConnectionPoints := TConnectionPoints.Create(Self);
- if AutoFactory.EventTypeInfo <> nil then
- FConnectionPoints.CreateConnectionPoint(AutoFactory.EventIID, ckMulti,
- EventConnect);
- // Route main form memo's OnChange event to MemoChange method:
- MainForm.Memo.OnChange := MemoChange;
- // Register this object with COM's Running Object Table (ROT) so other
- // clients can connect to this instance.
- RegisterActiveObject(Self as IUnknown, Class_ServerWithEvents,
- ACTIVEOBJECT_WEAK, FObjRegHandle);
- end;
-
- procedure TServerWithEvents.Clear;
- var
- EC: IEnumConnections;
- ConnectData: TConnectData;
- Fetched: Cardinal;
- begin
- MainForm.Memo.Lines.Clear;
- EC := GetConnectionEnumerator;
- if EC <> nil then
- begin
- while EC.Next(1, ConnectData, @Fetched) = S_OK do
- if ConnectData.pUnk <> nil then
- (ConnectData.pUnk as IServerWithEventsEvents).OnClear;
- end;
- end;
-
- procedure TServerWithEvents.AddText(const NewText: WideString);
- begin
- MainForm.Memo.Lines.Add(NewText);
- end;
-
- procedure TServerWithEvents.MemoChange(Sender: TObject);
- var
- EC: IEnumConnections;
- ConnectData: TConnectData;
- Fetched: Cardinal;
- begin
- EC := GetConnectionEnumerator;
- if EC <> nil then
- begin
- while EC.Next(1, ConnectData, @Fetched) = S_OK do
- if ConnectData.pUnk <> nil then
- (ConnectData.pUnk as IServerWithEventsEvents).OnTextChanged((Sender as TMemo).Text);
- end;
- end;
-
- function TServerWithEvents.GetConnectionEnumerator: IEnumConnections;
- var
- Container: IConnectionPointContainer;
- CP: IConnectionPoint;
- begin
- Result := nil;
- OleCheck(QueryInterface(IConnectionPointContainer, Container));
- OleCheck(Container.FindConnectionPoint(AutoFactory.EventIID, CP));
- CP.EnumConnections(Result);
- end;
-
- initialization
- TAutoObjectFactory.Create(ComServer, TServerWithEvents,
- Class_ServerWithEvents, ciMultiInstance, tmApartment);
- end.
-